package com.siwei.mes.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.siwei.mes.common.base.BaseParams;
import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.entity.ProjectArea;
import com.siwei.mes.entity.User;
import com.siwei.mes.mapper.ProjectAreaMapper;
import com.siwei.mes.mapper.UserMapper;
import com.siwei.mes.service.ProjectAreaService;
import com.siwei.mes.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 用户表(User)表服务实现类
 *
 * @author linzi
 * @since 2023-11-25 18:16:11
 */
@Service
public class ProjectAreaServiceImpl implements ProjectAreaService {
    @Resource
    private ProjectAreaMapper projectAreaMapper;

    @Override
    public Long create(ProjectArea entity) {
        entity.init(true);
        assertTrue(null!=projectAreaMapper.selectByProjectArea(entity.getProjectArea()),"区域名称已经存在");
        projectAreaMapper.insertSelective(entity);
        return entity.getId();
    }

    @Override
    public void update(ProjectArea entity) {
        entity.init(false);
        ProjectArea user = projectAreaMapper.selectByPrimaryKey(entity.getId());
        assertTrue(user == null, "工程区域划分信息不存在");
        projectAreaMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void delete(Long id) {
        ProjectArea user = projectAreaMapper.selectByPrimaryKey(id);
        assertTrue(user == null, "工程区域划分不存在");
        projectAreaMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<User> getPage(PageQuery<ProjectArea, ProjectArea> query) {
        ProjectArea searchParam = query.getParams();
        return PageHelper.startPage(query.getPageNum(), query.getPageSize())
                .doSelectPageInfo(() -> projectAreaMapper.getList(searchParam));
    }

    @Override
    public ProjectArea loadById(Long id) {
        return projectAreaMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<ProjectArea> getAll(ProjectArea entity) {
        return projectAreaMapper.getList(entity);
    }
}
